# ------------------------------------------------------------------------------
# Cargo Build Stage
# ------------------------------------------------------------------------------

FROM accuminium/rust-ubuntu:20.10_stable as cargo-build
USER root
ARG https_proxy=http://10.110.36.59:3128
ARG http_proxy=http://10.110.36.59:3128
RUN mkdir -p /opt


COPY linuxx64_odbc_cli.tar.gz /opt/clidriver.tar.gz
WORKDIR /opt
RUN tar zxvf /opt/clidriver.tar.gz
RUN rm -f /opt/clidriver.tar.gz

RUN  sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN  apt-get clean


ARG OPENSSL_VERSION=1.1.1i
RUN https_proxy=http://10.110.36.59:3128 http_proxy=http://10.110.36.59:3128  apt-get update
RUN DEBIAN_FRONTEND=noninteractive https_proxy=http://10.110.36.59:3128 http_proxy=http://10.110.36.59:3128  apt-get install -y  --no-install-recommends git libxml2-dev gcc openssl  libssl-dev build-essential cmake  graphviz libpq-dev libsqlite-dev linux-libc-dev pkgconf  xutils-dev unixodbc-dev && apt-get clean && rm -rf /var/cache/apk/*
RUN git config --global https.proxy http://10.110.36.59:3128

RUN https_proxy=http://10.110.36.59:3128 rustup default stable
RUN https_proxy=http://10.110.36.59:3128 rustup target add x86_64-unknown-linux-gnu
ARG CARGO_CONFIG="/usr/local/cargo/config"
RUN mkdir -p /usr/local/cargo
COPY docker/.cargo/config /usr/local/cargo/
RUN export CARGO_HTTP_DEBUG=true
RUN export CARGO_LOG=cargo::ops::registry=debug
RUN mkdir -p /usr/src/test_rust_actix_db2
WORKDIR /usr/src/test_rust_actix_db2
COPY Cargo.toml Cargo.toml
RUN mkdir src/
ARG CARGO_HTTP_MULTIPLEXING=false
ARG CARGO_HTTP_CHECK_REVOKE=false


RUN echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs
RUN https_proxy=http://10.110.36.59:3128 RUSTFLAGS=-Clinker=gcc cargo build -v --release --target=x86_64-unknown-linux-gnu
RUN rm -f target/x86_64-unknown-linux-gnu/release/deps/test_rust_actix_db2*
COPY src ./src
RUN https_proxy=http://10.110.36.59:3128 RUSTFLAGS=-Clinker=gcc cargo rustc -v --release --target=x86_64-unknown-linux-gnu -- -C link-arg="-L/opt/clidriver/lib"
# ------------------------------------------------------------------------------
# Final Stage
# ------------------------------------------------------------------------------

FROM ubuntu:20.10
USER root


COPY linuxx64_odbc_cli.tar.gz /opt/clidriver.tar.gz
WORKDIR /opt
RUN tar zxvf /opt/clidriver.tar.gz
RUN rm -f /opt/clidriver.tar.gz


RUN  sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN  apt-get clean



RUN https_proxy=http://10.110.36.59:3128 http_proxy=http://10.110.36.59:3128  apt-get update

RUN DEBIAN_FRONTEND=noninteractive https_proxy=http://10.110.36.59:3128 http_proxy=http://10.110.36.59:3128 apt-get install -y libxml2-dev unixodbc-dev  && apt-get clean && rm -rf /var/cache/apk/*


WORKDIR /opt
COPY --from=cargo-build /usr/src/test_rust_actix_db2/target/x86_64-unknown-linux-gnu/release/test_rust_actix_db2 .

EXPOSE 8080
ENTRYPOINT LD_LIBRARY_PATH=/opt/clidriver/lib  ./test_rust_actix_db2
